package com.qiezi.project.fragment;



import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.qiezi.R;
import com.qiezi.base.BaseMvpFragment;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

public class TabsFragment extends BaseMvpFragment {

    public enum TabsType{
        home(0),recommend(1);

        private int type;
        TabsType(int t){
            type = t;
        }

        public int getType() {
            return type;
        }
    }


    private TabsType type;

    public TabsFragment setType(TabsType type){
        this.type =type;
        return this;
    }


    private class PagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> mFragments = new ArrayList();
        private final String[] mTitles;

        public PagerAdapter(FragmentManager manager,String[] titles,Fragment[] fragments) {
            super(manager);
            Collections.addAll(mFragments,fragments);
            mTitles = titles;
        }

        public int getCount() {
            return this.mFragments.size();
        }

        public Fragment getItem(int paramInt) {
            return mFragments.get(paramInt);
        }

        public CharSequence getPageTitle(int paramInt) {
            return this.mTitles[paramInt];
        }
    }

    @BindView(R.id.tabs)
    SlidingTabLayout slidingTabLayout;

    @BindView(R.id.pager)
    ViewPager vp;


    public void showFirstPage(){
        slidingTabLayout.setCurrentTab(0,false);
        allScrollToTop();
    }

    public void allScrollToTop(){
        for (int i=0;i<adapter.getCount();i++){
            ((BaseMvpFragment)adapter.getItem(i)).scrollToTop();
        }
    }


    @Override
    public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        if (savedInstanceState == null)return;
        savedInstanceState.putInt("index",slidingTabLayout.getCurrentTab());
        savedInstanceState.putInt("type",type.getType());
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_home;
    }

    PagerAdapter adapter;


    private RecyListFragment home;


    @Override
    protected void initData(Bundle savedInstanceState) {
        int index = 0;
        if (savedInstanceState != null){
            int t = savedInstanceState.getInt("type");
            type = TabsType.values()[t];
            index = savedInstanceState.getInt("index");
        }


        String[] titles;
        Fragment[] fragments;

        if (type == TabsType.home){
            home = new RecyListFragment().setType(RecyListFragment.HomePageType.home);
            titles = new String[]{"首页", "精选", "最新", "排行"};
            fragments = new Fragment[]{
                    home,
                    new RecyListFragment().setType(RecyListFragment.HomePageType.domestic),
                    new RecyListFragment().setType(RecyListFragment.HomePageType.newest),
                    new RecyListFragment().setType(RecyListFragment.HomePageType.ranking)
            };

        }else{

            titles = new String[]{"国产推荐","日韩推荐","欧美推荐"};
            fragments = new Fragment[]{
                    new RecyListFragment().setType(RecyListFragment.HomePageType.domesticRecommend),
                    new RecyListFragment().setType(RecyListFragment.HomePageType.jsRecommend),
                    new RecyListFragment().setType(RecyListFragment.HomePageType.eaRecommend)
            };

        }

        adapter = new PagerAdapter(getChildFragmentManager(),titles,fragments);
        vp.setAdapter(adapter);
        slidingTabLayout.setViewPager(vp);
        if (index > 0){
            slidingTabLayout.setCurrentTab(index,false);
        }
    }



    @Override
    protected void initListener() {
        slidingTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                log(position);
                if (type == TabsType.home){
                    if (position != 0){
                        home.stopBanner();
                    }else {
                        home.startBanner();
                    }
                }
            }

            @Override
            public void onTabSelectScrollComplete(int position) {

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }
}
